{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "df5c0f28",
   "metadata": {},
   "source": [
    "## 1. API 现货交易账户接口总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57b6d650",
   "metadata": {},
   "source": [
    "### 1.1 现货账户接口"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f952aeca",
   "metadata": {},
   "source": [
    "|方法|解释|Path|\n",
    "|:---|:---|:---|\n",
    "|get_account|账户信息|/api/v3/account|\n",
    "|get_myTrades|账户成交历史|/api/v3/myTrades|\n",
    "|get_rateLimit_order|查询目前下单数|/api/v3/rateLimit/order|\n",
    "|get_myPreventedMatches|获取 Prevented Matches|/api/v3/myPreventedMatches|\n",
    "|get_myAllocations|查询分配结果|/api/v3/myAllocations|\n",
    "|get_account_commission|查询佣金费率|/api/v3/account/commission|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11964a3d",
   "metadata": {},
   "source": [
    "### 1.2 现货交易接口"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9b683f9",
   "metadata": {},
   "source": [
    "|方法|解释|Path|\n",
    "|:---|:---|:---|\n",
    "|set_order_test|测试下单|/api/v3/order/test|\n",
    "|set_order|下单|/api/v3/order|\n",
    "|cancel_order|撤销订单|/api/v3/order|\n",
    "|cancel_openOrders|撤销单一交易对的所有挂单|/api/v3/openOrders|\n",
    "|set_order_cancelReplace|撤消挂单再下单|/api/v3/order/cancelReplace|\n",
    "|get_order|查询订单|/api/v3/order|\n",
    "|get_openOrders|当前挂单|/api/v3/openOrders|\n",
    "|get_allOrders|查询所有订单|/api/v3/allOrders|\n",
    "|set_order_oco|OCO下单|/api/v3/order/oco|\n",
    "|cancel_orderList|取消 OCO 订单|/api/v3/orderList|\n",
    "|get_orderList|查询 OCO|/api/v3/orderList|\n",
    "|get_allOrderList|查询所有 OCO|/api/v3/allOrderList|\n",
    "|get_openOrderList|查询 OCO 挂单|/api/v3/openOrderList|\n",
    "|set_sor_order|下 SOR 订单|/api/v3/sor/order|\n",
    "|set_sor_order_test|测试 SOR 下单接口 (TRADE)|/api/v3/sor/order/test|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bed1a52",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a387f8f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.api import SPOT\n",
    "from binance_interface.app.utils import eprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7d05f5f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "spot = SPOT(\n",
    "    key=key, secret=secret,\n",
    "    proxy_host=proxy_host\n",
    ")\n",
    "account = spot.account\n",
    "trade = spot.trade"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01e62040",
   "metadata": {},
   "source": [
    "## 3. 账户相关"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31208d8e",
   "metadata": {},
   "source": [
    "### 3.1 账户信息 get_account"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "10dd24c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'makerCommission': 10,\n",
      "          'takerCommission': 10,\n",
      "          'buyerCommission': 0,\n",
      "          'sellerCommission': 0,\n",
      "          'commissionRates': {'maker': '0.00100000',\n",
      "                              'taker': '0.00100000',\n",
      "                              'buyer': '0.00000000',\n",
      "                              'seller': '0.00000000'},\n",
      "          'canTrade': True,\n",
      "          'canWithdraw': True,\n",
      "          'canDeposit': True,\n",
      "          'brokered': False,\n",
      "          'requireSelfTradePrevention': False,\n",
      "          'preventSor': False,\n",
      "          'updateTime': 1706192452814,\n",
      "          'accountType': 'SPOT',\n",
      "          'balances': [{'asset': 'BTC', 'free': '0.00000000', 'locked': '0.00000000'},\n",
      "                       {'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'},\n",
      "                       {'asset': 'ETH', 'free': '0.00439560', 'locked': '0.00000000'},\n",
      "                       {'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'},\n",
      "                       {'asset': 'BNB', 'free': '0.00000000', 'locked': '0.00000000'},\n",
      "                       '......'],\n",
      "          'permissions': ['SPOT'],\n",
      "          'uid': 459188049},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "account_result = account.get_account()\n",
    "eprint(account_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be073a6e",
   "metadata": {},
   "source": [
    "### 3.2 账户成交历史 get_myTrades"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6b82142e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'ETHUSDT',\n",
      "           'id': 1244422993,\n",
      "           'orderId': 14965084948,\n",
      "           'orderListId': -1,\n",
      "           'price': '2256.00000000',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'ETHUSDT',\n",
      "           'id': 1259771011,\n",
      "           'orderId': 15231603125,\n",
      "           'orderListId': -1,\n",
      "           'price': '2283.70000000',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'ETHUSDT',\n",
      "           'id': 1259845830,\n",
      "           'orderId': 15232742712,\n",
      "           'orderListId': -1,\n",
      "           'price': '2288.69000000',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'ETHUSDT',\n",
      "           'id': 1289453065,\n",
      "           'orderId': 15715801574,\n",
      "           'orderListId': -1,\n",
      "           'price': '2230.99000000',\n",
      "           '...': '......'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "myTrades_result = account.get_myTrades(symbol='ETHUSDT')\n",
    "eprint(myTrades_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "377fb41d",
   "metadata": {},
   "source": [
    "### 3.3 查询佣金费率 account_commission_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c06b9ff4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'BTCUSDT',\n",
      "          'standardCommission': {'maker': '0.00100000',\n",
      "                                 'taker': '0.00100000',\n",
      "                                 'buyer': '0.00000000',\n",
      "                                 'seller': '0.00000000'},\n",
      "          'taxCommission': {'maker': '0.00000000',\n",
      "                            'taker': '0.00000000',\n",
      "                            'buyer': '0.00000000',\n",
      "                            'seller': '0.00000000'},\n",
      "          'discount': {'enabledForAccount': True,\n",
      "                       'enabledForSymbol': True,\n",
      "                       'discountAsset': 'BNB',\n",
      "                       'discount': '0.75000000'}},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "account_commission_result = account.get_account_commission(symbol='BTCUSDT')\n",
    "eprint(account_commission_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cda7e4a4",
   "metadata": {},
   "source": [
    "## 4. 交易相关"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7530800a",
   "metadata": {},
   "source": [
    "### 4.1 下单 set_order"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6aebc7dd",
   "metadata": {},
   "source": [
    "#### 4.1.1 现货限单价购买"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9ea5b72d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'MANAUSDT',\n",
      "          'orderId': 2208870980,\n",
      "          'orderListId': -1,\n",
      "          'clientOrderId': 'HfMnOen9rke3toOsAxZRg3',\n",
      "          'transactTime': 1706245628583,\n",
      "          'price': '0.40000000',\n",
      "          'origQty': '30.00000000',\n",
      "          'executedQty': '0.00000000',\n",
      "          'cummulativeQuoteQty': '0.00000000',\n",
      "          'status': 'NEW',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'side': 'BUY',\n",
      "          'workingTime': 1706245628583,\n",
      "          'fills': [],\n",
      "          'selfTradePreventionMode': 'EXPIRE_MAKER'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "set_order_result = trade.set_order(\n",
    "    symbol='MANAUSDT',\n",
    "    price='0.4',\n",
    "    quantity='30',\n",
    "    side='BUY',\n",
    "    type='LIMIT',\n",
    "    timeInForce='GTC',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1ef98db",
   "metadata": {},
   "source": [
    "#### 4.1.2 现货市单价购买"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5c6d3b6b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'MANAUSDT',\n",
      "          'orderId': 2208871142,\n",
      "          'orderListId': -1,\n",
      "          'clientOrderId': 'cMVlAFyHDAS4BcqpxacOxx',\n",
      "          'transactTime': 1706245648192,\n",
      "          'price': '0.00000000',\n",
      "          'origQty': '15.00000000',\n",
      "          'executedQty': '15.00000000',\n",
      "          'cummulativeQuoteQty': '6.58200000',\n",
      "          'status': 'FILLED',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'MARKET',\n",
      "          'side': 'BUY',\n",
      "          'workingTime': 1706245648192,\n",
      "          'fills': [{'price': '0.43880000',\n",
      "                     'qty': '15.00000000',\n",
      "                     'commission': '0.01500000',\n",
      "                     'commissionAsset': 'MANA',\n",
      "                     'tradeId': 199546482}],\n",
      "          'selfTradePreventionMode': 'EXPIRE_MAKER'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "set_order_result = trade.set_order(\n",
    "    symbol='MANAUSDT',\n",
    "    quantity='15',\n",
    "    side='BUY',\n",
    "    type='MARKET',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a145d13c",
   "metadata": {},
   "source": [
    "#### 4.1.3 现货限单价卖出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "71cbb95e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'MANAUSDT',\n",
      "          'orderId': 2208871186,\n",
      "          'orderListId': -1,\n",
      "          'clientOrderId': 'JQJOEHsnf31nxtQK7CD0QZ',\n",
      "          'transactTime': 1706245662898,\n",
      "          'price': '0.80000000',\n",
      "          'origQty': '10.00000000',\n",
      "          'executedQty': '0.00000000',\n",
      "          'cummulativeQuoteQty': '0.00000000',\n",
      "          'status': 'NEW',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'side': 'SELL',\n",
      "          'workingTime': 1706245662898,\n",
      "          'fills': [],\n",
      "          'selfTradePreventionMode': 'EXPIRE_MAKER'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "set_order_result = trade.set_order(\n",
    "    symbol='MANAUSDT',\n",
    "    price='0.8',\n",
    "    quantity='10',\n",
    "    side='SELL',\n",
    "    type='LIMIT',\n",
    "    timeInForce='GTC',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9806cdc3",
   "metadata": {},
   "source": [
    "#### 4.1.4 现货市单价卖出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d2e8a7db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'MANAUSDT',\n",
      "          'orderId': 2208871525,\n",
      "          'orderListId': -1,\n",
      "          'clientOrderId': 'OF0MJsZIR8skLw8mpo09xv',\n",
      "          'transactTime': 1706245720662,\n",
      "          'price': '0.00000000',\n",
      "          'origQty': '15.00000000',\n",
      "          'executedQty': '15.00000000',\n",
      "          'cummulativeQuoteQty': '6.57300000',\n",
      "          'status': 'FILLED',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'MARKET',\n",
      "          'side': 'SELL',\n",
      "          'workingTime': 1706245720662,\n",
      "          'fills': [{'price': '0.43820000',\n",
      "                     'qty': '15.00000000',\n",
      "                     'commission': '0.00657300',\n",
      "                     'commissionAsset': 'USDT',\n",
      "                     'tradeId': 199546484}],\n",
      "          'selfTradePreventionMode': 'EXPIRE_MAKER'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "set_order_result = trade.set_order(\n",
    "    symbol='MANAUSDT',\n",
    "    quantity='15',\n",
    "    side='SELL',\n",
    "    type='MARKET',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8200be8",
   "metadata": {},
   "source": [
    "### 4.2 当前挂单 get_openOrders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "85658fde",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'MANAUSDT',\n",
      "           'orderId': 2208870980,\n",
      "           'orderListId': -1,\n",
      "           'clientOrderId': 'HfMnOen9rke3toOsAxZRg3',\n",
      "           'price': '0.40000000',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'MANAUSDT',\n",
      "           'orderId': 2208871186,\n",
      "           'orderListId': -1,\n",
      "           'clientOrderId': 'JQJOEHsnf31nxtQK7CD0QZ',\n",
      "           'price': '0.80000000',\n",
      "           '...': '......'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "openOrderList_result = trade.get_openOrders(symbol='')\n",
    "eprint(openOrderList_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b573c69d",
   "metadata": {},
   "source": [
    "### 4.3 查询订单 get_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "508bcc2d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'MANAUSDT',\n",
      "          'orderId': 2208870980,\n",
      "          'orderListId': -1,\n",
      "          'clientOrderId': 'HfMnOen9rke3toOsAxZRg3',\n",
      "          'price': '0.40000000',\n",
      "          'origQty': '30.00000000',\n",
      "          'executedQty': '0.00000000',\n",
      "          'cummulativeQuoteQty': '0.00000000',\n",
      "          'status': 'NEW',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'side': 'BUY',\n",
      "          'stopPrice': '0.00000000',\n",
      "          'icebergQty': '0.00000000',\n",
      "          'time': 1706245628583,\n",
      "          'updateTime': 1706245628583,\n",
      "          'isWorking': True,\n",
      "          'workingTime': 1706245628583,\n",
      "          'origQuoteOrderQty': '0.00000000',\n",
      "          'selfTradePreventionMode': 'EXPIRE_MAKER'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "order_result = trade.get_order(\n",
    "    symbol='MANAUSDT',\n",
    "    orderId='2208870980',\n",
    ")\n",
    "eprint(order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6679806",
   "metadata": {},
   "source": [
    "### 4.3 撤销订单 cancel_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a74bd8d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'MANAUSDT',\n",
      "          'origClientOrderId': 'HfMnOen9rke3toOsAxZRg3',\n",
      "          'orderId': 2208870980,\n",
      "          'orderListId': -1,\n",
      "          'clientOrderId': 'C7v6cEUWrVAOsvgJ85vAln',\n",
      "          'transactTime': 1706245758514,\n",
      "          'price': '0.40000000',\n",
      "          'origQty': '30.00000000',\n",
      "          'executedQty': '0.00000000',\n",
      "          'cummulativeQuoteQty': '0.00000000',\n",
      "          'status': 'CANCELED',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'side': 'BUY',\n",
      "          'selfTradePreventionMode': 'EXPIRE_MAKER'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "cancel_order_result = trade.cancel_order(\n",
    "    symbol='MANAUSDT',\n",
    "    orderId='2208870980'\n",
    ")\n",
    "eprint(cancel_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6d29719",
   "metadata": {},
   "source": [
    "### 4.4 撤销单一交易对的所有挂单 cancel_openOrders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bd916c07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'MANAUSDT',\n",
      "           'origClientOrderId': 'JQJOEHsnf31nxtQK7CD0QZ',\n",
      "           'orderId': 2208871186,\n",
      "           'orderListId': -1,\n",
      "           'clientOrderId': '4oJvZysoac8AcPk1SQ798A',\n",
      "           '...': '......'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "cancel_openOrders_result = trade.cancel_openOrders(symbol='MANAUSDT')\n",
    "eprint(cancel_openOrders_result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
